<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>动态组件</title>
    <style>
        .app {
            font-family: sans-serif;
            border: 1px solid #eee;
            border-radius: 2px;
            padding: 20px 30px;
            margin-top: 1em;
            margin-bottom: 40px;
            user-select: none;
            overflow-x: auto;
        }

        .tab-button {
            padding: 6px 10px;
            border-top-left-radius: 3px;
            border-top-right-radius: 3px;
            border: 1px solid #ccc;
            cursor: pointer;
            background: #f0f0f0;
            margin-bottom: -1px;
            margin-right: -1px;
        }

        .tab-button:hover {
            background: #e0e0e0;
        }

        .tab-button.active {
            background: #e0e0e0;
        }

        .demo-tab {
            border: 1px solid #ccc;
            padding: 10px;
        }
    </style>
    <script src='./vue.global.js'></script>
</head>

<body>
    <div id='app'>
        <button v-for="tab in tabs" class="tab-button" @click="currentTab = tab">{{tab}}</button>

        <!-- 
        动态组件 
        :is="currentTabComponent" 当前要动态渲染的组件名为【currentTabComponent数据对应的组件名】
        正常情况下 切换新的组件时 旧的组件要卸载 新的组件要挂载
        keep-alive包裹动态组件时 再切换组件 组件不会执行卸载/挂载 而是直接调用上一次的渲染缓存
        -->
        <keep-alive>
            <component :is="currentTabComponent"></component>
        </keep-alive>

    </div>



    <script>
        const app = Vue.createApp({
            data() {
                return {
                    tabs: ["home", "posts", "archive"],
                    currentTab: "home"
                }
            },
            methods: {},
            computed: {
                /* 依赖的一手数据currentTab只要不变 就使用上一次的 */
                currentTabComponent() {
                    return "tab-" + this.currentTab
                }
            }
        })

        /* tab-home */
        app.component("tab-home", {
            template: `<div class="demo-tab">Home Component</div>`,
            mounted(){
                console.log("tab-home mounted");
            },
            unmounted(){
                console.log("tab-home unmounted");
            },
        })

        /* tab-posts */
        app.component("tab-posts", {
            data() {
                return {
                    posts: [
                        {
                            id: 1,
                            title: "薛定谔的猫",
                            content: "Dinge Xue's HelloKitty consectetur adipisicing elit. Sequi, error tempora! Aut deserunt, omnis cumque illum veniam nobis in odio quis fugit illo, quos alias tempora ut perspiciatis magnam molestias."
                        },
                        {
                            id: 2,
                            title: "哥的狗狗",
                            content: "My Puppy amet consectetur adipisicing elit. Sequi, error tempora! Aut deserunt, omnis cumque illum veniam nobis in odio quis fugit illo, quos alias tempora ut perspiciatis magnam molestias."
                        },
                        {
                            id: 3,
                            title: "碧竹苑的你",
                            content: "Somebody is very beautiful amet consectetur adipisicing elit. Sequi, error tempora! Aut deserunt, omnis cumque illum veniam nobis in odio quis fugit illo, quos alias tempora ut perspiciatis magnam molestias."
                        },
                    ],

                    // 当前选中的博文
                    selectedPost: null
                }
            },
            template: `<div class="demo-tab">
                <!-- 博文列表 -->
                <ul>
                    <li v-for="post in posts" @click="selectedPost = post">{{post.title}}</li>
                </ul>

                <!-- 博客正文 -->
                <div>
                    <p>{{selectedPost?selectedPost.content:"今晚打麻将"}}</p>
                </div>
            </div>`
        })

        /* tab-archive */
        app.component("tab-archive", {
            template: `<div class="demo-tab">Archive Component</div>`
        })

        app.mount('#app')
    </script>
</body>

</html>